<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/2/27
 * Time: 10:07
 */
namespace app\shop\api;

use app\common\api\BaseApi;
use app\shop\model\Favorites;
use app\extend\Page;

class FavoritesApi extends BaseApi{
    /**
     * 添加
     */
    const ADD = "shop/FavoritesApi/add";
    /**
     * 查询一条数据
     */
    const GET_INFO = "shop/FavoritesApi/getInfo";
    /**
     * 删除
     */
    const DELETE = "shop/FavoritesApi/delete";
    /**
     * 统计
     */
    const COUNT = "shop/FavoritesApi/count";
    /**
     * 收藏帖子分页
     */
    const QUERY_THREAD_WITH_COUNT = "shop/FavoritesApi/queryThreadWithCount";
    /**
     * 收藏商品分页
     */
    const QUERY_PRODUCT_WITH_COUNT = "shop/FavoritesApi/queryProductWithCount";
    /**
     * 收藏MMZ分页
     */
    const QUERY_GIFT_WITH_COUNT = "shop/FavoritesApi/queryGiftWithCount";

    protected function _init(){
        $this -> model = new Favorites();
    }
    public function queryGiftWithCount($map = null, $page = ['curpage'=>1,'size'=>10], $order = false, $params = false, $fields = false){
        $main_img = 6015;
        $query = $this-> model->alias('f')-> join("left join itboye_product as p on f.favorite_id = p.id")-> join('left join common_member as m on m.uid = p.uid') -> join("(select pid,img_id as main_img from itboye_product_image where type=".$main_img.") as image on favorite_id=image.pid","left") -> join('(select id as c_id,name as cate_name from itboye_category ) c on p.cate_id=c.c_id','left') -> join('(select view_cnt,pid as pro_id from itboye_product_attr where is_second=1) a on f.favorite_id=a.pro_id','left') -> join('(select uid as win_uid,goods_id,win_time,evaluate_time,express_time from itboye_mmz_winner_pepole ) wp on f.favorite_id=wp.goods_id','left');
        $start = max(intval($page['curpage'])-1,0)*intval($page['size']);
        // $query = $query -> page($page['curpage'] . ',' . $page['size']);
        $query = $query -> limit($start,$page['size']);

        if(!empty($map)) $query = $query -> where($map);
        if(false !== $order) $query = $query->order($order);
        if(false !== $fields) $query = $query->field($fields);
        $list = $query -> select();

        if (false === $list) return $this -> apiReturnErr($this -> model -> getDbError());
        $count = $this -> model->alias('f')-> join("itboye_product as p on f.favorite_id = p.id") -> where($map) -> count();
        return $this -> apiReturnSuc(["count" => $count, "list" => $list]);
    }
    public function queryProductWithCount($map = null, $page = ['curpage'=>0,'size'=>10], $order = false, $params = false, $fields = false){

        $query = $this -> model;
        $main_img = 6015;

        $query = $query -> join("(select id,name,onshelf,status,cate_id from itboye_product) as product on favorite_id=product.id") -> join("(select pid,total_sales from itboye_product_attr) as attr on favorite_id=attr.pid","left") -> join("(select pid,img_id as img from itboye_product_image where type=".$main_img.") as image on favorite_id=image.pid","left") -> join("(select product_id,min(price) as price,max(ori_price) as ori_price from itboye_product_sku group by product_id) as sku on favorite_id=sku.product_id","left");
        $query = $query -> page($page['curpage'] . ',' . $page['size']);

        if(!is_null($map)){
            $map['status'] = 1;
            $map['onshelf'] = 1;
            $query = $query -> where($map);
        }
        if(false !== $order) $query = $query->order($order);
        if(false !== $fields) $query = $query->field($fields);
        $list = $query -> select();

        if (false === $list) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }


        $count = $this -> model -> join("(select id,name,onshelf,status from itboye_product) as product on favorite_id=product.id") -> join("(select pid,img_id from itboye_product_image where type=".$main_img.") as image on favorite_id=image.pid","left") -> join("(select product_id,min(price) as price from itboye_product_sku group by product_id) as sku on favorite_id=sku.product_id","left") -> where($map) -> count();
        return $this -> apiReturnSuc(["count" => $count, "list" => $list]);

    }


    public function queryThreadWithCount($map = null, $page = ['curpage'=>0,'size'=>10], $order = false, $params = false, $fields = false){

        $query = $this->model;
        $query = $query -> join("itboye_forum_thread on favorite_id=itboye_forum_thread.tid");
        $query = $query -> page($page['curpage'] . ',' . $page['size']);

        if(!is_null($map)){
            $map['status'] = 0; //正常的帖子
            $query = $query -> where($map);
        }
        if(false !== $order) $query = $query->order($order);
        if(false !== $fields) $query = $query->field($fields);
        $list = $query -> select();

        if (false === $list) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> join("itboye_forum_thread on favorite_id=itboye_forum_thread.tid") -> where($map) -> count();
        // // 查询满足要求的总记录数
        // $Page = new \think\Page($count, $page['size']);

        // //分页跳转的时候保证查询条件
        // if (false !== $params) {
        //     foreach ($params as $key => $val) {
        //         $Page -> parameter[$key] = urlencode($val);
        //     }
        // }
        // // 实例化分页类 传入总记录数和每页显示的记录数
        // $show = $Page -> show();
        return $this -> apiReturnSuc(["count" => $count, "list" => $list]);

    }

}